<?php


namespace api\modules\v1\controllers\buss;

use common\models\base\CommonStatus;
use services\base\CommonStatusService;
use services\buss\PlatformService;
use services\member\MemberService;
use Yii;
use api\controllers\OnAuthController;
use common\helpers\CommonHelper;
use services\buss\TargetAccountsService;

class TargetAccountsController extends OnAuthController
{
    public $modelClass = '';

    /**
     * @api {get} /v1/buss/target-accounts/index 对标账号列表
     * @apiGroup TargetAccounts
     * @apiName index
     * @apiUse CommonParam
     *
     * @apiParam {String} created_stime             创建开始时间
     * @apiParam {String} created_etime             创建结束时间
     * @apiParam {String} keywords                  对标账号名
     * @apiParam {int} platform_id                  平台ID
     * @apiParam {int} t_id                         账号类型ID
     * @apiParam {int} status                       状态
     * @apiParam {int} created_member_id            创建人ID
     * @apiParam {int} page
     * @apiParam {int} pageSize
     *
     * @apiSuccess {Object}  data
     * @apiSuccess {Array}   data.list                              列表数据
     * @apiSuccess {Number}  data.list.id                           ID
     * @apiSuccess {String}  data.list.name                         对标账号
     * @apiSuccess {Number}  data.list.accounts_id                  系统账号ID
     * @apiSuccess {String}  data.list.accounts_name                系统账号
     * @apiSuccess {Number}  data.list.t_id                         账号类型ID
     * @apiSuccess {String}  data.list.t_name                       账号类型
     * @apiSuccess {String}  data.list.position                     人设定位
     * @apiSuccess {Number}  data.list.platform_id                  平台ID
     * @apiSuccess {String}  data.list.platform_name                平台
     * @apiSuccess {Number}  data.list.fans_num                     粉丝数
     * @apiSuccess {Number}  data.list.works_num                    作品数
     * @apiSuccess {Number}  data.list.created_member_id            创建人ID
     * @apiSuccess {String}  data.list.created_member_name          创建人
     * @apiSuccess {String}  data.list.change_date                  更新日期
     * @apiSuccess {String}  data.list.created_at_str               创建日期
     * @apiSuccess {String}  data.list.created_at
     * @apiSuccess {String}  data.list.updated_at
     * @apiSuccess {String}  data.condition
     * @apiSuccess {String}  data.condition.platform_arr                     平台筛选
     * @apiSuccess {String}  data.condition.account_type_arr                 账号类型筛选
     * @apiSuccess {String}  data.condition.created_member_arr               创建人筛选
     * @apiSuccess {String}  data.condition.account_arr                      系统账号筛选
     *
     */
    public function actionIndex()
    {
        $params = Yii::$app->request->get();

        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;
        $targetAccountService = new TargetAccountsService();
        $list = $targetAccountService->getList($params, $page, $pageSize);

        // 筛选项
        $list['condition'] = [
            'platform_arr' => PlatformService::getListCondition(),
            'account_type_arr' => CommonStatusService::getListCondition(CommonStatus::TYPE_ACCOUNTS),
            'created_member_arr' => TargetAccountsService::getCreatedMemberCondition(),
        ];
        return $list;
    }

    /**
     * @api {get} /v1/buss/target-accounts/info     详情数据
     * @apiGroup TargetAccounts
     * @apiName info
     * @apiUse CommonParam
     *
     * @apiParam {int} id                对标账号ID
     *
     * @apiSuccess {Object}  data
     * @apiSuccess {Number}  id
     * @apiSuccess {String}  name
     * @apiSuccess {Number}  accounts_id
     * @apiSuccess {Number}  t_id
     * @apiSuccess {String}  position
     * @apiSuccess {Number}  platform_id
     * @apiSuccess {Number}  fans_num
     * @apiSuccess {Number}  works_num
     * @apiSuccess {Array}   data.condition
     * @apiSuccess {Array}   data.condition.account_arr                     系统账号
     * @apiSuccess {Array}   data.condition.account_type_arr                账号类型
     * @apiSuccess {Array}   data.condition.platform_arr                    平台
     *
     */
    public function actionInfo(): array
    {
        $params = Yii::$app->request->get();
        $id = $params['id'] ?? 0;

        // 查询结果
        $service = new TargetAccountsService();
        return $service->getInfo((int)$id);
    }
    /**
     * @api {post} /v1/buss/target-accounts/update 更新对标账号
     * @apiGroup TargetAccounts
     * @apiName update
     * @apiUse CommonParam
     *
     * @apiBody {Number}   id                                               对标账号ID【编辑时必传】
     * @apiBody {Number}   accounts_id                                      系统账号
     * @apiBody {String}   name                                             对标账号名称
     * @apiBody {Number}   t_id                                             账号类型
     * @apiBody {String}   position                                         人设定位
     * @apiBody {Number}   platform_id                                      平台ID
     * @apiBody {Number}   fans_num                                         粉丝数
     * @apiBody {Number}   works_num                                        作品数
     * @apiBody {Number}   status                                           状态
     *
     * @apiSuccess {Number} data    新增/编辑ID
     */
    public function actionUpdate()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['name']) || !$params['name']) {
            return CommonHelper::api(422, '对标账号必填');
        }
        $params['id'] = $params['id'] ?? 0;
        $targetAccountService = new TargetAccountsService();
        return $targetAccountService->updateTargetAccounts($params);
    }

    /**
     * @api {post} /v1/buss/target-accounts/delete 删除对标账号
     * @apiGroup TargetAccounts
     * @apiName delete
     * @apiUse CommonParam
     *
     * @apiBody {String} id     ID
     *
     * @apiSuccess {Object} data    返回数据
     */
    public function actionDelete()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['id']) || !$params['id']) {
            return CommonHelper::api(422, '缺少数据ID');
        }

        $targetAccountService = new TargetAccountsService();
        return $targetAccountService->deleteTargetAccounts($params);
    }
}
